// components/calendar/index.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    // 日历的年份
    sendYear: {
      type: Number,
      value: new Date().getFullYear()
    },
    // 日历的月份
    sendMonth: {
      type: Number,
      value: new Date().getMonth() + 1
    },
    // 已签到日期, ['2021-03-01', '2021-03-02'], 
    dataSource: {
      type: Array,
      value: [new Date().getFullYear() + '-' + ('0' + (new Date().getMonth() + 1)).slice(-2) + '-' + ('0' + (new Date().getDate())).slice(-2)]
    },
    // 是否显示往日未签到样式
    showSigned: {
      type: Boolean,
      value: true
    },
    // 是否显示今日未签到样式
    showTodaySigned: {
      type: Boolean,
      value: false
    },
    langType: {
      type: String,
      value: 'ch'
    }
  },
  observers: {
    'dataSource'(data) {
      this.onResChange(data)
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    days: [],
    SignUp: [],
    cur_year: 0, //当前选的年
    cur_month: 0, //当前选的月
    today: parseInt(new Date().getDate()), //本日
    toMonth: parseInt(new Date().getMonth() + 1), //本月
    toYear: parseInt(new Date().getFullYear()), //本年
    weeks_ch: ['日', '一', '二', '三', '四', '五', '六'],
    weeks_en: ['Sun', 'Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat'],
    selectDay: 0,
  },

  lifetimes: {
    attached() {
      this.setData({
        cur_year: this.data.sendYear,
        cur_month: this.data.sendMonth,
        SignUp: this.data.dataSource,
      })
  
      this.calculateEmptyGrids(this.data.cur_year, this.data.cur_month);
      this.calculateDays(this.data.cur_year, this.data.cur_month);
      this.onJudgeSign();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 获取当月共多少天
    getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate()
    },
    // 获取当月第一天星期几
    getFirstDayOfWeek(year, month) {
      return new Date(Date.UTC(year, month - 1, 1)).getDay();
    },
    // 计算当月1号前空了几个格子，把它填充在days数组的前面
    calculateEmptyGrids(year, month) {
      console.log('calculateEmptyGrids')
      //计算每个月时要清零
      this.setData({
        days: []
      })
      let days = []
      const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
      if (firstDayOfWeek > 0) {
        for (let i = 0; i < firstDayOfWeek; i++) {
          var obj = {
            date: null,
            isSign: false
          }
          days.push(obj);
        }
        this.setData({
          days: days
        })
      }
    },
    // 绘制当月天数占的格子，并把它放到days数组中
    calculateDays(year, month) {
      const thisMonthDays = this.getThisMonthDays(year, month);
      let days = this.data.days
      // this.columnsLen=Math.ceil(thisMonthDays/7);
      // console.log(this.columnsLen);
      for (let i = 1; i <= thisMonthDays; i++) {
        var obj = {
          date: i,
          isSign: false
        }
        days.push(obj);
      }
      this.setData({
        days: days
      })
      //console.log(this.days);
    },
    onResChange(newD) {
      // this.SignUp = newD;
      this.setData({
        SignUp: newD
      })
      this.onJudgeSign();
    },
    //匹配判断当月与当月哪些日子签到打卡
    onJudgeSign() {
      var signs = this.data.SignUp;
      var daysArr = this.data.days;
      for (var i = 0; i < signs.length; i++) {
        var current = new Date(signs[i].replace(/-/g, "/"));
        var year = current.getFullYear();
        var month = current.getMonth() + 1;
        var day = current.getDate();
        day = parseInt(day);
        for (var j = 0; j < daysArr.length; j++) {
          //年月日相同则打卡成功   						
          if (year == this.data.cur_year && month == this.data.cur_month && daysArr[j].date == day) { //&& signs[i].isSign == "今日已打卡"
            // console.log(daysArr[j].date, day);
            daysArr[j].isSign = true;
          }
        }
      }
      // this.days = daysArr;
      this.setData({
        days: daysArr
      })
    },
    // 切换控制年月，上一个月，下一个月
    handleCalendar(type) {
      const cur_year = parseInt(this.cur_year);
      const cur_month = parseInt(this.cur_month);
      var newMonth;
      var newYear = cur_year;
      if (type === 0) { //上个月
        newMonth = cur_month - 1;
        if (newMonth < 1) {
          newYear = cur_year - 1;
          newMonth = 12;
        }
      } else {
        newMonth = cur_month + 1;
        if (newMonth > 12) {
          newYear = cur_year + 1;
          newMonth = 1;
        }
      }
      this.calculateEmptyGrids(newYear, newMonth);
      this.calculateDays(newYear, newMonth);

      // this.cur_year = newYear;
      // this.cur_month = newMonth;

      // this.SignUp = []; //先清空
      this.setData({
        cur_year: newYear,
        cur_month: newMonth,
        SignUp: []
      })
      // this.$emit('dateChange', this.cur_year+"-"+this.cur_month); //传给调用模板页面去拿新数据
      this.triggerEvent('dateChange', this.data.cur_year+"-"+this.data.cur_month)			
    },
    clickSignUp(date, type) { //type=0补签，type=1当日签到		
			
      var str = "签到";
      if (type == 0) {//如果不需要补签功能直接在这阻止不执行后面的代码就行。
        str = "补签";
      }
      // uni.showToast({
      //   title: str + "成功" + date + "号",
      //   icon: 'success',
      //   duration: 2000
      // });
      // this.SignUp.push(this.cur_year + "-" + this.cur_month + "-" + date); //自动加假数据，写了接口就不用了
      
      // console.log(this.SignUp);
      // this.$forceUpdate();
      
      // this.$emit('clickChange', this.cur_year + "-" + this.cur_month + "-" + date); //传给调用模板页面
      this.triggerEvent('clickChange', this.data.cur_year + "-" + this.data.cur_month + "-" + date)

      //refresh
      this.onJudgeSign();
    },
    clickDate(e) {
      console.log(e)
      // this.setData({
      //   selectDay: e.currentTarget.dataset.date
      // })
      // this.triggerEvent('clickLack', e.currentTarget.dataset.date)
    },
  }
})
